import { reactive, inject } from "vue";
import { useProjectStore } from '../../../store/useProjectStore'

export const useTaskTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;

    const taskData = reactive(
        {
            total: 0,
            tableData: [],
            taskDoList:[],
            childTaskList:[],
            queryCondition: {
                taskName: '',
            }
        }
    );

    const projectStore = useProjectStore();

    //获取负责人下的任务
    const getTasksOfPerson = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.GetTasksOfPerson,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { taskName: taskData.queryCondition.taskName },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                taskData.tableData = response.data.data;
                taskData.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //根据任务ID获取完成记录
    const getTaskDosByTaskId = async (taskId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetTaskDosByTaskId,
                {
                    taskId: taskId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                taskData.taskDoList = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //添加任务完成情况
    const createTaskDo = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.CreateTaskDo,
                projectStore.taskDoModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //更新任务完成情况
    const updateTaskDo = async (model) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.UpdateTaskDo,
                model
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

     //删除任务完成情况
     const delTaskDoById = async (id) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.DelTaskDoById,
                {
                    id:id
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //根据任务ID递归获取该任务下所有子任务
    const getDeepChildTasksByPId = async (taskId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetDeepChildTasksByPId,
                {
                    taskId: taskId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                taskData.childTaskList = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };


    return {
        taskData,
        projectStore,
        getTasksOfPerson,
        getTaskDosByTaskId,
        createTaskDo,
        updateTaskDo,
        delTaskDoById,
        getDeepChildTasksByPId
    };
};